package br.sc.senai.view;

import java.awt.Dimension;

import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

import javax.swing.JButton;
import javax.swing.ImageIcon;

import br.sc.senai.dao.ClienteDAO;
import br.sc.senai.model.Cliente;
import br.sc.senai.util.FormatarTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class EditarClienteUI extends JInternalFrame {
	private JTextField textField;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private ClienteDAO clienteDAO = new ClienteDAO();
	private JRadioButton rdbtnFsica_1;
	private JRadioButton rdbtnJurdica_1;
	private static EditarClienteUI instancia;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;

	public static EditarClienteUI obterInstancia(Cliente cliente) {
		if (instancia == null) {
			instancia = new EditarClienteUI(cliente);
		}
		return instancia;
	}

	public EditarClienteUI(Cliente cliente) {
		setTitle("Editar Cliente");
		
		Dimension d = PrincipalUI.obterInstancia().getSize();
		d.setSize(d.getWidth(), (d.getHeight() - 100));
		setBounds(0, 80, 1024, 768);
		this.setMinimumSize(d);

		setClosable(true);
		setResizable(false);

		textField = new JTextField();
		textField.setBounds(95, 23, 374, 19);
		textField.setColumns(10);

		JButton btnIncluir = new JButton("Editar");
		btnIncluir.setBounds(12, 226, 105, 34);
		btnIncluir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Cliente cliente = new Cliente();
				cliente.setNome(textField.getText());
				if(rdbtnFsica_1.isSelected()){
					cliente.setCpf(textField_1.getText());
				}else{
					cliente.setCnpj(textField_1.getText());
				}
				cliente.setEndereco(textField_2.getText());
				cliente.setCidade(textField_3.getText());
				cliente.setCep(textField_4.getText());
				cliente.setTelefone(textField_5.getText());
				cliente.setEmail(textField_6.getText());
				clienteDAO.alterar(cliente);
			}
		});
		btnIncluir.setIcon(new ImageIcon(EditarClienteUI.class.getResource("/br/sc/senai/icones/editar.png")));

		JLabel lblNome = new JLabel("Nome:");
		lblNome.setBounds(38, 25, 45, 15);

		textField_1 = new JFormattedTextField(FormatarTextField.Mascara("###.###.###-##"));
		textField_1.setEditable(false);
		textField_1.setBounds(95, 62, 193, 19);
		textField_1.setColumns(10);

		final JLabel lblCpf = new JLabel("CPF:");
		lblCpf.setEnabled(false);
		lblCpf.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCpf.setBounds(29, 64, 54, 15);

		textField_2 = new JTextField();
		textField_2.setBounds(95, 105, 374, 19);
		textField_2.setColumns(10);

		JLabel lblEndereo = new JLabel("Endereço:");
		lblEndereo.setBounds(12, 107, 71, 15);

		JLabel lblCidade = new JLabel("Cidade:");
		lblCidade.setBounds(29, 144, 54, 15);

		textField_3 = new JTextField();
		textField_3.setBounds(95, 142, 193, 19);
		textField_3.setColumns(10);

		JLabel lblCep = new JLabel("CEP:");
		lblCep.setBounds(306, 144, 31, 15);

		textField_4 = new JFormattedTextField(FormatarTextField.Mascara("#####-###"));
		textField_4.setBounds(349, 142, 120, 19);
		textField_4.setColumns(10);

		JLabel lblTelefone = new JLabel("Telefone:");
		lblTelefone.setBounds(16, 183, 67, 15);

		textField_5 = new JFormattedTextField(FormatarTextField.Mascara("(##) ####-####"));
		textField_5.setBounds(95, 181, 114, 19);
		textField_5.setColumns(10);

		textField_6 = new JTextField();
		textField_6.setBounds(299, 181, 170, 19);
		textField_6.setColumns(10);

		JLabel lblEmail = new JLabel("E-mail:");
		lblEmail.setBounds(227, 183, 54, 15);

		rdbtnFsica_1 = new JRadioButton("Física");
		rdbtnFsica_1.setEnabled(false);
		buttonGroup.add(rdbtnFsica_1);
		rdbtnFsica_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblCpf.setText("CPF:");
				textField_1.requestFocus();
			}
		});
		rdbtnFsica_1.setSelected(true);
		rdbtnFsica_1.setBounds(306, 60, 65, 23);

		rdbtnJurdica_1 = new JRadioButton("Jurídica");
		rdbtnJurdica_1.setEnabled(false);
		buttonGroup.add(rdbtnJurdica_1);
		rdbtnJurdica_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lblCpf.setText("CNPJ:");
				textField_1.requestFocus();
			}
		});
		rdbtnJurdica_1.setBounds(392, 60, 77, 23);
		getContentPane().setLayout(null);
		getContentPane().add(lblEndereo);
		getContentPane().add(lblCidade);
		getContentPane().add(lblTelefone);
		getContentPane().add(lblNome);
		getContentPane().add(lblCpf);
		getContentPane().add(textField_3);
		getContentPane().add(lblCep);
		getContentPane().add(textField_4);
		getContentPane().add(textField_2);
		getContentPane().add(textField_1);
		getContentPane().add(rdbtnFsica_1);
		getContentPane().add(rdbtnJurdica_1);
		getContentPane().add(textField_5);
		getContentPane().add(lblEmail);
		getContentPane().add(textField_6);
		getContentPane().add(textField);
		getContentPane().add(btnIncluir);
		
		textField.setText(cliente.getNome());
		
		System.out.println(cliente.getCpf());
		System.out.println(cliente.getCnpj());
		if (cliente.getCpf() != null){
			System.out.println("É fisico");
			textField_1.setText(cliente.getCpf());
			rdbtnFsica_1.setSelected(true);
			rdbtnJurdica_1.setSelected(false);
		}else{

			System.out.println("É juridico");
			textField_1.setText(cliente.getCnpj());
			rdbtnFsica_1.setSelected(false);
			rdbtnJurdica_1.setSelected(true);
		}
		textField_2.setText(cliente.getEndereco());
		textField_3.setText(cliente.getCidade());
		textField_4.setText(cliente.getCep());
		textField_5.setText(cliente.getTelefone());
		textField_6.setText(cliente.getEmail());

		JButton btnFechar = new JButton("Fechar");
		btnFechar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnFechar.setIcon(new ImageIcon(EditarClienteUI.class
				.getResource("/br/sc/senai/icones/Undo_24x24.png")));
		btnFechar.setBounds(349, 231, 120, 34);
		getContentPane().add(btnFechar);
	}
}
